home *** CD-ROM | disk | FTP | other *** search
/ Aminet 19 / Aminet 19 (1997)(GTI - Schatztruhe)[!][Jun 1997].iso / Aminet / comm / net / ARemote1_00B.lha / ARemote / source / divert.timer.c < prev    next >
C/C++ Source or Header  |  1997-04-27  |  8KB  |  284 lines

  1.  
  2. #include <devices/timer.h>
  3.  
  4. #include <exec/libraries.h>
  5. #include <exec/memory.h>
  6. #include <libraries/commodities.h>
  7. #include <dos/dos.h>
  8. #include <clib/exec_protos.h>
  9. #include <clib/alib_protos.h>
  10. #include <clib/alib_stdio_protos.h>
  11. #include <clib/commodities_protos.h>
  12. #include <devices/inputevent.h>
  13. #include <clib/AMarquee_protos.h>
  14. #include <pragmas/AMarquee_pragmas.h>
  15.  
  16. int main(int, char **);
  17. void ProcessMsg(CxObj * broker, struct MsgPort * broker_mp, CxObj * cocustom);
  18. void CxFunction(CxMsg *, CxObj *);
  19.  
  20. struct Library * AMarqueeBase = NULL;
  21. struct QSession * session     = NULL;
  22. struct Library * CxBase, *IconBase, * TimerBase;
  23. int port = 20000;
  24. BOOL BConnectOkay = FALSE;
  25. struct timerequest * TimerIO;
  26. struct MsgPort * event_mp;
  27.  
  28. char * connectTo = "";  /* IP name to connect to, or "" if we should go passive instead */
  29. char * hosts = "";
  30.  
  31. struct NewBroker newbroker =
  32.   NB_VERSION,
  33.   "Divert",
  34.   "Divert",
  35.   "show divert",
  36.   NBU_UNIQUE | NBU_NOTIFY,
  37.   0, 0, 0, 0
  38. };
  39.  
  40. /* Makes pcString lower case */
  41. char * ToLower(char * pcString)
  42. {
  43.     char * pcTemp = pcString;
  44.     
  45.     while(*pcTemp) 
  46.     {
  47.         if ((*pcTemp >= 'A')&&(*pcTemp <= 'Z'))
  48.             *pcTemp += 'a'-'A';
  49.         pcTemp++;
  50.     }
  51.     return(pcString);
  52. }
  53.  
  54. void edebug(int i)
  55. {
  56.   int a = *((int *)i);
  57. }
  58.  
  59. /* return TRUE iff the string represents the boolean value true */
  60. BOOL ParseBool(char * string)
  61. {
  62.   char temp[100];
  63.   
  64.   strncpy(temp,string,sizeof(temp));
  65.   temp[99]=0;
  66.   
  67.   ToLower(temp);
  68.   
  69.   return(
  70.     (*temp=='\0')              ||  /* then it's just KEYWORD, so yes, right? */
  71.     (strcmp("true",temp) == 0) ||
  72.     (strcmp("yes",temp)  == 0) ||
  73.     (strcmp("1",temp)    == 0));   /* Anyone who specifies 1 for true is a weenie! */
  74. }
  75.  
  76.  
  77. int main(int argc, char ** argv)
  78. {
  79.   UBYTE ** ttypes;
  80.   CxMsg * msg;
  81.   
  82.   if (NULL == (TimerIO = (struct timerequest *)AllocMem(sizeof(struct timerequest), MEMF_PUBLIC | MEMF_CLEAR)))
  83.   {
  84.     printf("Couldn't allocate timerequest\n");
  85.     exit(10);
  86.   }
  87.   if (OpenDevice(TIMERNAME, UNIT_MICROHZ, TimerIO, 0))
  88.   {
  89.     printf("Couldn't open timer.device\n");
  90.     FreeMem(TimerIO, sizeof(struct timerequest));
  91.     exit(10);
  92.   }
  93.   TimerBase = (struct Library *) TimerIO->tr_node.io_Device;
  94.   
  95.   if (AMarqueeBase = OpenLibrary("amarquee.library",42L))
  96.   {
  97.     if (CxBase = OpenLibrary("commodities.library", 37L))
  98.     {
  99.       if (IconBase = OpenLibrary("icon.library", 36L))
  100.       {
  101.         struct MsgPort * broker_mp;
  102.         CxObj * broker, * cocustom;
  103.  
  104.         if (event_mp = CreateMsgPort())
  105.         {
  106.           if (broker_mp = CreateMsgPort())
  107.           { 
  108.             newbroker.nb_Port = broker_mp;
  109.         
  110.             ttypes = ArgArrayInit(argc, argv);
  111.             newbroker.nb_Pri = (BYTE) ArgInt(ttypes, "CX_PRIORITY", 0);
  112.         
  113.             connectTo = ArgString(ttypes, "CONNECT", "");
  114.             hosts     = ArgString(ttypes, "ACCEPT",  "");
  115.             port      = ArgInt(ttypes,    "PORT", port);
  116.             
  117.             if (broker = CxBroker(&newbroker, NULL))
  118.             {
  119.               if (cocustom = CxCustom(CxFunction, 0L))
  120.               {
  121.                 AttachCxObj(broker, cocustom);
  122.                 ActivateCxObj(broker, 1L);
  123.                 ProcessMsg(broker, broker_mp, cocustom);
  124.               }
  125.               DeleteCxObjAll(broker);          
  126.               while(msg = (CxMsg *) GetMsg(broker_mp)) ReplyMsg((struct Message *)msg);  
  127.             }
  128.             DeletePort(broker_mp);
  129.           }
  130.         
  131.           while(msg = (CxMsg *) GetMsg(event_mp)) FreeMem(msg, ((struct Message *)msg)->mn_Length);
  132.           DeletePort(event_mp);
  133.         }
  134.       
  135.         ArgArrayDone();
  136.         CloseLibrary(IconBase);
  137.       }
  138.       CloseLibrary(CxBase);
  139.     }
  140.     CloseLibrary(AMarqueeBase);
  141.   }
  142.   
  143.   /* free timer.device */
  144.   CloseDevice((struct IORequest *)TimerIO);
  145.   FreeMem(TimerIO, sizeof(struct timerequest));
  146.   return(0);
  147. }
  148.  
  149. void Disconnect()
  150. {
  151.   if (session) QFreeSession(session);
  152.   session = NULL;
  153.   BConnectOkay = FALSE;
  154. }
  155.  
  156. void Reconnect()
  157. {
  158.   LONG pPort = port;
  159.   
  160.   Disconnect();
  161.   
  162.   if (*connectTo) printf("Connecting to [%s]\n",connectTo);
  163.              else printf("Accepting connections from [%s]\n",hosts);
  164.              
  165.   if (*connectTo) session = QNewSessionAsync(connectTo, pPort, "ARemote");
  166.              else session = QNewHostSession(hosts, &pPort, "ARemote");
  167. }
  168.  
  169. void ProcessMsg(CxObj * broker, struct MsgPort * broker_mp, CxObj * cocustom)
  170. {
  171.   struct Message * msg;
  172.   CxMsg * cmsg;
  173.   ULONG sigrcvd, msgid;
  174.   BOOL keepGoing = TRUE;
  175.  
  176.   Reconnect();  /* attempt an initial connection */
  177.      
  178.   while(keepGoing)
  179.   {
  180.     sigrcvd = Wait(SIGBREAKF_CTRL_C | (1L<<broker_mp->mp_SigBit) | (1L<<event_mp->mp_SigBit) | (session ? (1L<<session->qMsgPort->mp_SigBit) : 0L));
  181.     
  182.     while(cmsg = (CxMsg *)GetMsg(broker_mp))
  183.     {
  184.       msgid = CxMsgID(cmsg);
  185.       ReplyMsg((struct Message *)cmsg);
  186.       
  187.       switch(msgid)
  188.       {
  189.         case CXCMD_DISABLE: printf("disabled.\n"); ActivateCxObj(broker, 0L); break;
  190.         case CXCMD_ENABLE:  printf("enabled.\n");  ActivateCxObj(broker, 1L); break;
  191.         case CXCMD_KILL:    printf("kill.\n");     keepGoing = FALSE;         break;
  192.         case CXCMD_UNIQUE:  printf("uniqued.\n");  keepGoing = FALSE;         break;
  193.       }
  194.     }
  195.  
  196.     /* handle incoming InputEvent messages from our custom CxObject */
  197.     {
  198.       BOOL BSent = FALSE;
  199.  
  200.       while(msg = (struct Message *)GetMsg(event_mp))
  201.       {
  202.         if ((session)&&(BConnectOkay))
  203.         {
  204.           struct InputEvent * ie = (struct InputEvent *) (((UBYTE *)msg)+sizeof(struct Message));
  205.       
  206.           /* Send a copy of the Input Event to our buddy */
  207.           (void)QStreamOp(session, "e", ie, sizeof(struct InputEvent));
  208.           BSent = TRUE;
  209.         }
  210.         FreeMem(msg, msg->mn_Length);
  211.       }
  212.       if (BSent) QGo(session, 0L);
  213.     }
  214.  
  215.     if (session)
  216.     {
  217.       struct QMessage * qmsg;
  218.       
  219.       while(qmsg = (struct QMessage *)GetMsg(session->qMsgPort))
  220.       { 
  221.         if (qmsg->qm_Status == QERROR_NO_ERROR)
  222.         {
  223.           if (qmsg->qm_ID == 0) 
  224.           {
  225.             printf("Connection to %s established.\n",qmsg->qm_Path);
  226.             BConnectOkay = TRUE;
  227.           }
  228.           else
  229.           {
  230.             if (qmsg->qm_DataLen == sizeof(struct InputEvent))
  231.             { 
  232.               struct InputEvent * ie = (struct InputEvent *)qmsg->qm_Data;
  233. /*              GetSysTime(&(ie->ie_TimeStamp));  */
  234.               AddIEvents(ie);
  235.             }
  236.             else printf("Error, bad data size (%i != %i)\n",qmsg->qm_DataLen, sizeof(struct InputEvent));
  237.           }
  238.         }
  239.         else
  240.         {
  241.           printf("Error %i received, disconnecting\n", qmsg->qm_Status);
  242.           FreeQMessage(session, qmsg);  /* Gotta free this NOW!  Before the session is gone */
  243.           Disconnect();
  244.           break;  /* Can't evaluate while() condition again!  session is dangling! */
  245.         }
  246.         FreeQMessage(session, qmsg);
  247.       }
  248.     }
  249.     
  250.     if (sigrcvd & SIGBREAKF_CTRL_C) {printf("CTRL-C detected.\n"); keepGoing = FALSE;}
  251.   }
  252.   
  253.   if (session) 
  254.   {
  255.     QFreeSession(session);
  256.     session = NULL;
  257.   }
  258. }
  259.  
  260. __geta4 void CxFunction(register CxMsg * cxm, CxObj * co)
  261. {
  262.   struct InputEvent * ie;
  263.   
  264.   ie = (struct InputEvent *)CxMsgData(cxm);
  265.  
  266.   if ((ie->ie_Class == IECLASS_RAWKEY)     ||
  267.       (ie->ie_Class == IECLASS_RAWMOUSE)   ||
  268.       (ie->ie_Class == IECLASS_POINTERPOS)) 
  269.   {
  270.      /* Send a message containing a copy of this InputEvent to our program for transmission */
  271.      int bufLen = sizeof(struct Message)+sizeof(struct InputEvent);
  272.      struct Message * msg = (struct Message *)AllocMem(bufLen, MEMF_PUBLIC | MEMF_CLEAR);
  273.        
  274.      if (msg)
  275.      {
  276.        msg->mn_Node.ln_Type = NT_MESSAGE;
  277.        msg->mn_Length       = bufLen;
  278.        msg->mn_ReplyPort    = NULL;   /* No reply needed; we'll just free it */
  279.        memcpy(((UBYTE *)msg)+sizeof(struct Message), ie, sizeof(struct InputEvent));
  280.        PutMsg(event_mp, msg);
  281.      } 
  282.   }
  283. }